package net.wangds.yunz.initialize.flow;

import net.wangds.auth.menu.conv.Res2SysMenuConv;
import net.wangds.auth.resource.comp.GeneralMenuComp;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.common.base.funcs.Functions;
import net.wangds.jpa.Sorts;
import net.wangds.log.helper.LogHelper;
import net.wangds.procengine.ProcResEnum;
import net.wangds.procengine.flow.FlowContext;
import net.wangds.procengine.flow.define.FlowDef;
import net.wangds.procengine.flow.define.actor.ActorDef;
import net.wangds.procengine.flow.define.node.FlowNode;
import net.wangds.procengine.flow.define.node.JavaFlowNode;
import net.wangds.procengine.flow.instance.actor.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/10/18 10:01.
 */
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class StepInitMenus  extends JavaFlowNode {


    public static final String KEY_MENUS = "MENUS";

    private Res2SysMenuConv mmConvRes2Sys = new Res2SysMenuConv();

    @Autowired
    private GeneralMenuComp mmCmpMenu;



    public <A extends ActorDef> StepInitMenus(FlowDef<A> flow,
                                              BiFunction<FlowContext, Actor, ProcResEnum> func,
                                              FlowNode previousNode) {
        super(flow, func, previousNode);
    }

    public <A extends ActorDef> StepInitMenus(FlowDef<A> flow,
                                              FlowNode previousNode) {
        super(flow, previousNode);
        this.setFunc(this::proc);
    }

    private ProcResEnum proc(FlowContext ctx, Actor actor) {

        LogHelper.dev("清理现有菜单");
        //首先删除所有菜单资源
        mmCmpMenu.findAll(Sorts.createTm_desc).forEach(mmCmpMenu::erase);

        LogHelper.dev("初始化现有菜单");
        Functions.ofList(
                //GeneralResourceEntity.ofMenu("发布资讯", "", "console", "news", "publish-news", "gotoPublishNews", 0),
                //GeneralResourceEntity.ofMenu("媒体资源", "", "console", "news", "media-management", "gotoMediaManagement", 0),

                //监控和告警系统
                ////监控地图
                GeneralResourceEntity.ofMenu("监控地图", "", "monitor", "map",
                        "monitor-map", "gotoMonitorMap", 0),
                ////监控数据
                //GeneralResourceEntity.ofMenu("监控数据", "", "monitor", "data",
                //        "monitor-data", "gotoMonitorData", 0),
                ////告警数据
                GeneralResourceEntity.ofMenu("告警数据", "", "monitor", "data",
                        "monitor-warms", "gotoWarmData", 0),
                ////运营商信息
                GeneralResourceEntity.ofMenu("厂商信息", "", "monitor", "info",
                        "monitor-operators", "gotoOperatorList", 0),
                ////充电站信息
                GeneralResourceEntity.ofMenu("站点信息", "", "monitor", "info",
                        "monitor-stations", "gotoStationList", 0),
                ////设备信息
                GeneralResourceEntity.ofMenu("设备信息", "", "monitor", "info",
                        "monitor-equipments", "gotoEquipmentList", 0),
                ////接口信息
                GeneralResourceEntity.ofMenu("接口信息", "", "monitor", "info",
                        "monitor-connectors", "gotoConnectorList", 0),


                //专家库模块
                //  Config.of(CONFIG_MENU_LABLE_CATA, "problems","专家库", 0, "")
                GeneralResourceEntity.ofMenu("常见问题", "", "problems", "mng",
                        "problem-management", "gotoProblemList", 0),
                GeneralResourceEntity.ofMenu("设备文档", "", "problems", "mng",
                        "equipment-docu", "gotoEquipmentDocuList", 0),

                GeneralResourceEntity.ofMenu("问题分类", "", "problems", "mng",
                        "err-cata", "gotoErrCataList", 0),

                //备品备件模块
                // Config.of(CONFIG_MENU_LABLE_CATA, "spareparts","备品备件", 0, "")
                GeneralResourceEntity.ofMenu("品类管理", "", "spareparts", "check",
                        "spareparts-management", "gotoSpareSpartsList", 0),
                GeneralResourceEntity.ofMenu("库存明细", "", "spareparts", "check",
                        "spareparts-check", "gotoSpareSpartsCheck", 0),
                GeneralResourceEntity.ofMenu("进出库记录", "", "spareparts", "check",
                        "spareparts-details", "gotoSpareSpartsAccountList", 0),
                GeneralResourceEntity.ofMenu("盘点记录", "", "spareparts", "check",
                        "spareparts-check-his", "gotoSpareSpartsCheckHis", 0),



                //系统管理模块
                GeneralResourceEntity.ofMenu("账户管理", "", "console", "system",
                        "account-management", "gotoAccountManagement", 0),
                GeneralResourceEntity.ofMenu("用户管理", "", "console", "system",
                        "user-management", "gotoUserManagement", 0),
                GeneralResourceEntity.ofMenu("部门管理", "", "console", "system",
                        "department-management", "gotoDepartmentManagement", 0),
                GeneralResourceEntity.ofMenu("职位管理", "", "console", "system",
                        "job-management", "gotoJobManagement", 0),
                GeneralResourceEntity.ofMenu("角色管理", "", "console", "system",
                        "role-management", "gotoRoleManagement", 0),
                GeneralResourceEntity.ofMenu("菜单管理", "", "console", "system",
                        "menu-management", "gotoMenuManagement", 0),
                GeneralResourceEntity.ofMenu("资源管理", "", "console", "system",
                        "res-management", "gotoResManagement", 0),

                GeneralResourceEntity.ofMenu("基本设置", "", "console", "config", "conf-basic", "gotoConfBasic", 0),
                GeneralResourceEntity.ofMenu("系统模块", "", "console", "config", "sub-sys", "gotoConfSubSys", 0),
                GeneralResourceEntity.ofMenu("字典数据", "", "console", "config", "dict-info", "gotoConfDict", 0),
                //GeneralResourceEntity.ofMenu("文玩分类", "", "console", "config", "art-cata", "gotoArtCata", 0),
                //GeneralResourceEntity.ofMenu("支付接口", "", "console", "config", "pay-api", "gotoPayApi", 0),
                GeneralResourceEntity.ofMenu("系统特性", "", "console", "config", "sys-feature", "gotoSysFeature", 0),
                GeneralResourceEntity.ofMenu("其他设置", "", "console", "config", "conf-orther", "gotoConfOther", 0),
                GeneralResourceEntity.ofMenu("常用工具", "", "console", "config", "tools", "gotoTools", 0)


                //GeneralResourceEntity.ofMenu("iOS 设置", "", "console", "app", "ios", "gotoAppIosConf", 0),
                //GeneralResourceEntity.ofMenu("安卓设置", "", "console", "app", "android", "gotoAppAndroidConf", 0),
                //GeneralResourceEntity.ofMenu("首页设置", "", "console", "app", "mainpage", "gotoMainPageConf", 0)

        ).forEach(
                mmCmpMenu::save
        );

        ctx.putBean(KEY_MENUS, mmCmpMenu.findAll(Sorts.createTm_desc).stream()
                .map(res->mmConvRes2Sys.convert(res)).collect(Collectors.toList()));

        return ProcResEnum.CONTINUE;
    }


}
